package com.iot08.common.utils;

import java.math.BigDecimal;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version 1.0
 * @Description: 字符串相关操作工具
 * @time: 2020/9/22 17:31
 * @author: Rock
 */
public class StringUtil {

    /**
     * 获取uuid
     *
     * @time: 2020/9/25 9:01
     * @author: Rock
     * @version 1.0
     */
    public static String getUuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 判空
     *
     * @param [value]
     * @time: 2020/9/23 12:02
     * @author: Rock
     * @version 1.0
     */
    public static boolean isBlank(Object value) {
        if (value == null) {
            return true;
        }
        int len = value.toString().length();
        if (len == 0) {
            return true;
        } else {
            for (int i = 0; i < len; ++i) {
                if (!Character.isWhitespace(value.toString().charAt(i))) {
                    return false;
                }
            }
            return true;
        }
    }

    /**
     * 判空
     *
     * @param [value]
     * @time: 2020/9/23 16:40
     * @author: Rock
     * @version 1.0
     */
    public static boolean isNotBlank(Object value) {
        return !isBlank(value);
    }

    /**
     * 判断是否为数字，包括小数、负数
     *
     * @param value
     * @time: 2021/1/28 18:35
     * @author: Rock
     * @version 1.0
     */
    public static boolean isNumeric(Object value) {
        if (value == null) {
            return false;
        }
        if (value instanceof Double || value instanceof Integer || value instanceof Long) {
            return true;
        } else {
            String pattern = "^[\\+\\-]?[\\d]+(\\.[\\d]+)?$";
            Pattern r = Pattern.compile(pattern);
            Matcher m = r.matcher(String.valueOf(value));
            return m.matches();
        }
    }

    public static void main(String[] args) {
        System.out.println("空串: " +StringUtil.isNumeric(""));
        System.out.println("空格: " +StringUtil.isNumeric(" "));
    }
}
